package club.dmyang.service;

import club.dmyang.common.enums.RespCodeEnums;
import club.dmyang.common.exception.HyBlogException;
import club.dmyang.dao.CategoryMapper;
import club.dmyang.dao.TagMapper;
import club.dmyang.entity.Category;
import club.dmyang.entity.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by brss on 2018/7/14.
 */
@Service("tagService")
public class TagServiceImpl implements TagService {

    private static final Logger LOGGER = LoggerFactory.getLogger(TagServiceImpl.class);

    @Autowired
    private TagMapper tagMapper;
    @Autowired
    private ArticleService articleService;


    /**
     * 根据主键查询一条标签
     *
     * @param tid
     * @return
     */
    @Override
    public Tag findOneById(Integer tid) {
        return tagMapper.selectByPrimaryKey(tid);
    }

    /**
     * 根据页面和每页容量来查询标签
     *
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Tag> findByPageAndLimit(Integer page, Integer limit) {
        if ( page < 1 || limit < 1 ){
            LOGGER.error("page或limit错误！[ page = "+page + "limit = "+limit + "]");
            throw new HyBlogException(RespCodeEnums.GLOBAL_CODE_PARAMETER_ERROR);
        }
        List<Tag> tags = tagMapper.selectByPageAndLimit((page - 1) * limit, limit);
        return tags;
    }
    /**
     * 根据标签名查询标签
     * @param tname
     * @return
     */
    @Override
    public Tag findOneByTname(String tname) {
        Tag tag = tagMapper.selectByTname(tname);
        return tag;
    }

    /**
     * 联查tag_article,tag ,查询文章id为aid的文章所包含的所有id
     *
     * @param aid
     * @return
     */
    @Override
    public List<Tag> findByArticleId(Integer aid) {
        return tagMapper.selectByArticleId(aid);
    }

    /**
     * 查询所有标签的个数
     * （用于分页计算页数）
     *
     * @return
     */
    @Override
    public int findAllNums() {
        return tagMapper.selectAllNums();
    }

    @Override
    public List<Tag> findAll() {
        return tagMapper.selectAll();
    }
}
